package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 显示预约数据
     *
     * @param date
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //根据日期查询预约设置数据
        String dateBegin = date + "-" + 1;
        String dateEnd = date + "-" + 31;
        List<Map> list = orderSettingDao.getOrderSettingByMonth(dateBegin, dateEnd);

        return list;
    }

    /**
     * 修改 设置可预约人数
     *
     * @param orderSetting
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //获取预约日期
        Date orderDate = orderSetting.getOrderDate();
        //查询预约表中是否有数据
        long count = orderSettingDao.findCountByOrderDate(orderDate);
        if (count > 0) {
            //修改预约表中的可预约人数
            orderSettingDao.editNumberByDate(orderSetting);
        } else {
            //添加一条预约日期到表中
            orderSettingDao.add(orderSetting);
        }
    }

    /**
     *    用excl表多条预约管理
     * @param datas
     */

    @Override
    public void add(List<OrderSetting> datas) {
        for (OrderSetting data : datas) {
            Date orderDate = data.getOrderDate();
            long count = orderSettingDao.findCountByOrderDate(orderDate);
            if (count > 0) {
                orderSettingDao.editNumberByDate(data);
            }else{
                orderSettingDao.add(data);
            }
        }

    }
}
